define(["jquery", "JavaScript.Lang"], function($){
    ModuleCall = function (app, module, task) {
        this.app = app;
        this.module = module;
        this.task = task;
    }

    Request = function () {}
    Request.prototype = { 
        isJSON:true,
        useCache:true,
        context:null,
    
        beforeSend:function(){},
        success:function(){},
        error:function(){},
    
        invoke:function(call, urlParams, data, type){
            var url = this.buildURL(call, urlParams);

            var request = {
                url:url,
                type: type ? type : "GET",
                data:data
            }
            request.beforeSend = this.beforeSend;
            request.success = this.success;
            request.error = this.error;
            request.cahce = this.useCache;
        
            if(this.isJSON){
                request.dataType = "json";
            }
            if(this.context){
                request.context = this.context;
            }
        
            $.ajax(request);
        },
    
        buildURL:function (call, params){
            var baseUrl = $("base").attr("href");
            
            var url = String.format("{0}?&controller={1}.{2}&task={3}&ajax=true", baseUrl, call.app, call.module, call.task);
            var paramList = "";
            if(TypeOf(params) == "string"){
                paramList = "&" + params;
            } else{
                $.each(params, function(Key, Value){
                    paramList += "&" + Key + "=" + Value;
                });
            }
        
            url += paramList;
            console.log(url);
            return url;        
        }
    }
    
    return {
        ModuleCall:ModuleCall,
        Request:Request
    };

})


